﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscoveryLogic.UI.Layouts.ContentScreens;
using DiscoveryLogic.DataServices.DataSources;

namespace DiscoveryLogic.UI.Layouts.Controls
{
    public partial class TradeHistoryNewsItemMarker : UserControl
    {
        #region Private Variables

        private DateTime _newsDate;
        private int _id;
           
        #endregion

        #region Dependency Properties

        public static readonly DependencyProperty NewsTypeProperty =
            DependencyProperty.Register("NewsType", typeof (NewsTypes), typeof (TradeHistoryNewsItemMarker),
                                        new PropertyMetadata(NewsTypeChangedCallback));

        public static readonly DependencyProperty SymbolProperty =
            DependencyProperty.Register("Symbol", typeof (string), typeof (TradeHistoryNewsItemMarker),
                                        new PropertyMetadata(SymbolChangedCallback));

        public static readonly DependencyProperty ReflectionVisibleProperty =
            DependencyProperty.Register("ReflectionVisible", typeof (bool), typeof (TradeHistoryNewsItemMarker),
                                        new PropertyMetadata(ReflectionChangedCallback));

        public NewsTypes NewsType
        {
            get { return (NewsTypes) GetValue(NewsTypeProperty); }
            set
            {
                SetValue(NewsTypeProperty, value);
                SetIconColor((NewsTypes) value);
            }
        }

        public string Symbol
        {
            get { return (string) GetValue(SymbolProperty); }
            set
            {
                SetValue(SymbolProperty, value);
                SymbolLabel.Text = (string) value;
            }
        }

        public bool ReflectionVisible
        {
            get { return (bool) GetValue(ReflectionVisibleProperty); }
            set
            {
                SetValue(ReflectionVisibleProperty, value);
                ToggleReflectionVisibility((bool) value);
            }
        }

        static void NewsTypeChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs args)
        {
            TradeHistoryNewsItemMarker item = o as TradeHistoryNewsItemMarker;

            if (item != null)
            {
                item.SetIconColor((NewsTypes) args.NewValue);
            }
        }

        static void SymbolChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs args)
        {
            TradeHistoryNewsItemMarker item = o as TradeHistoryNewsItemMarker;

            if (item != null)
            {
                item.SymbolLabel.Text = (string) args.NewValue;
            }
        }

        static void ReflectionChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs args)
        {
            TradeHistoryNewsItemMarker item = o as TradeHistoryNewsItemMarker;

            if (item != null)
            {
                item.ToggleReflectionVisibility((bool) args.NewValue);
            }
        }

        #endregion

        #region Constructors

        public TradeHistoryNewsItemMarker()
        {
            _newsDate = new DateTime();
            InitializeComponent();
        }

        #endregion

        #region Get/Set


            public double Scale
            {
                get { return IconScale.ScaleX; }
                set 
                { 
                    IconScale.ScaleX = value;
                    IconScale.ScaleY = value;
                }
            }

        public DateTime NewsDate
        {
            get { return _newsDate; }
            set { _newsDate = value; }
        }

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        #endregion

        #region Helper Methods

            /// <summary>
            /// Set the color of the news item indicator based
            /// on the news type
            /// </summary>
            /// <param name="newsType"></param>
            public void SetIconColor(NewsTypes newsType)
            {
                WorldwideNews.Opacity = 0;
                IndustryNews.Opacity = 0;
                CompanyNews.Opacity = 0;
                TrivialNews.Opacity = 0;

                switch ((int)newsType)
                {
                    case 0:
                        WorldwideNews.Opacity = 1;
                        break;
                    case 1:
                        IndustryNews.Opacity = 1;
                        break;
                    case 2:
                        CompanyNews.Opacity = 1;
                        break;
                    case 3:
                        TrivialNews.Opacity = 1;
                        break;
                    default:
                        break;
                }
            }

        #endregion

        #region Public Methods

            /// <summary>
            /// Toggle visibility of the reflection of the icon
            /// </summary>
            /// <param name="show"></param>
             public void ToggleReflectionVisibility(bool show)
             {
                 if(show)
                 {
                     WorldwideNewsReflection.Visibility = Visibility.Visible;
                     IndustryNewsReflection.Visibility = Visibility.Visible;
                     CompanyNewsReflection.Visibility = Visibility.Visible;
                     TrivialNewsReflection.Visibility = Visibility.Visible;
                 }
                 else
                 {
                     WorldwideNewsReflection.Visibility = Visibility.Collapsed;
                     IndustryNewsReflection.Visibility = Visibility.Collapsed;
                     CompanyNewsReflection.Visibility = Visibility.Collapsed;
                     TrivialNewsReflection.Visibility = Visibility.Collapsed;
                 }
             }

        #endregion

        #region Event Handlers

             private void NewsItem_MouseEnter(object sender, MouseEventArgs e)
             {
                 if (newsItemEnter != null)
                 {
                     newsItemEnter(this, e);
                 }
             }

             private void NewsItem_MouseLeave(object sender, MouseEventArgs e)
             {
                 if (newsItemLeave != null)
                 {
                     newsItemLeave(this, e);
                 }
             }            

        #endregion

        #region Events

             public event MouseEventHandler newsItemEnter;
             public event MouseEventHandler newsItemLeave;

        #endregion
    }
}
